import React, { Component, lazy, Suspense } from 'react';
import { Link, BrowserRouter, Route, NavLink } from 'react-router-dom';

// 这种方式会直接全部把组件引入
// import Home from './Home';
// import About from './About';
import Loading from './Loading'

// 在react中,默认情况下,会把所有的路由全部引入,如果路由太多可能会引起性能问题
// 懒加载,用户使用的时候,才会调用该路由

// 但是用该方法,在注册路由的时候要使用<Suspense fallback={<Loading />}>,在还未返回加载路由的时候,显示该回调
const Home = lazy(()=> import('./Home') )
const About = lazy(()=> import('./About'))

export default class Demo extends Component {
    
    render() {
        // const {users} = this.state
        return (
            <div>
                <div className="row">
                    <div className="col-xs-offset-2 col-xs-8">
                        <div className="page-header"><h2>React Router Demo</h2></div>
                    </div>
                </div>
                    <div className="row">
                        <div className="col-xs-2 col-xs-offset-2">
                            <div className="list-group">

                                {/* react中靠路由链接切组件，编写路由链接 */}
                                
                                <NavLink className="list-group-item" to="/about">About</NavLink>
                                <NavLink className="list-group-item" to="/home">Home</NavLink>
                            </div>
                        </div>
                        <div className="col-xs-6">
                            <div className="panel">
                                <div className="panel-body">
                                    {/* 注册路由 */}
                                    <Suspense fallback={<Loading />}>
                                        <Route path='/about' component={About}/>
                                        <Route path='/Home' component={Home} />
                                    </Suspense>
                                </div>
                            </div>
                        </div>
                    </div>
            </div>
        );
    }
}
